# write by hasaki
# first edit on 2019/04/21
# last change on 2019/04/24
# 获取深度数据保存到本地
import json
from hashakisea.strategy.haStrategyTemplate import HaStrategy

class DepthMsg(HaStrategy):
    def __init__(self):     
        super(DepthMsg,self).__init__()
        self.strategy_name='depth msg'
        self.save_json_path='C:/Users/cchyp/Desktop/hasaki/quote/last_day/data.json'
        self.counter_save_time=0
    
    def next(self,data):
        bids_price=[]
        asks_price=[]
        bids_volume=[]
        asks_volume=[]
        for i,k in zip(data['bids'],data['asks']):
            bids_price.append(float(i[0]))
            asks_price.append(float(k[0]))
            bids_volume.append(float(i[1]))
            asks_volume.append(float(k[1]))
        
        sum_asks=round(sum(asks_volume),4)
        sum_bids=round(sum(bids_volume),4)
        bid_ask=round(sum_bids/sum_asks,2)

        if self.counter_save_time==0:
            # 读取
            with open(self.save_json_path,'r',encoding='utf-8') as json_file:
                json_data=json.load(json_file)

            # 更新数据
            json_data['bid_ask']=bid_ask

            # 写入
            with open(self.save_json_path,'w',encoding='utf-8') as json_file:
                json.dump(json_data,json_file,ensure_ascii=False)
        elif self.counter_save_time==12:
            self.counter_save_time=-1
        
        self.counter_save_time+=1
        
    def onOrder(self):
        pass

    def onTrade(self):
        pass